<?php

namespace App\Listeners\OpenShop;

use App\Events\OpenShop\SignSmsSentEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\OpenShop\User\RmsUserService;
use App\Support\ChangeSupport;

class SignSmsSentLogListener extends BaseListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param SignSmsSentEvent $event
     * @return void
     */
    public function handle(SignSmsSentEvent $event)
    {
        $order = OpenShopSignOrderModel::query()
                                       ->where('orderNo', $event->orderNo)
                                       ->first();
        if (null === $order) {
            return;
        }

        $body     = RmsUserService::getRmsUserInfoByUserIds([$event->rmsUserId]);
        $username = $body['data'][0]['username'] ?? "用户ID:{$event->rmsUserId}";

//        $log               = new OpenShopSignOrderLogModel;
//        $log->orderId      = $order->id;
//        $log->content      = "支付短信已发送";
//        $log->type         = OpenShopSignOrderLogModel::TYPE_SIGN_SMS_SENT;
//        $log->operatorName = $username;
//        $log->createdAt    = strtotime($event->datetime);
//        $log->updatedAt    = strtotime($event->datetime);
//        $log->createdByUid = $event->rmsUserId;
//        $log->updatedByUid = $event->rmsUserId;
//        $log->changes      = "✉️支付短信已发送";
//        $log->save();

        ChangeSupport::getInstance()->buildInsertChanges("✉️加盟成功短信已发送");
        ChangeSupport::getInstance()->batchChangesWriteToOrderLog(
            $order->id,
            OpenShopSignOrderLogModel::TYPE_SIGN_SMS_SENT,
            $username
        );
    }
}
